const { Service } = require("egg");
class GenericService extends Service {
  async create(tableName, data) {
    const result = await this.app.mysql.insert(tableName, data);
    return result;
  }
  async update(tableName, data) {
    let result = await this.app.mysql.update(tableName, data);
    return result;
  }
  async delete(tableName, id) {
    let result = await this.app.mysql.delete(tableName, { id });
    await result;
  }

  async findById(tableName, id) {
    let result = await this.app.mysql.get(tableName, { id });
    return result;
  }

  async findAll(tableName, where= {}, page = 1, limit = 10) {
    const offset = (page - 1) * limit;
    console.log(where);
    const results = await this.app.mysql.select(tableName, {
      where,
      limit,
      offset
    }); 
    return results;
  }
}
module.exports = GenericService;
